package com.scalar.forecast.projectSetup.actionCommands;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.projectSetup.MapPartNumbers;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.projectSetup.forms.MapPartNumbersForm;


/**
 * Process the request to display the project setup details which are present in the scope
 *
 * Created by Veeru.
 * Date: Oct 10, 2005
 * Time: 5:44:11 PM
 */

public class MapPartNumbersEditCommand extends ActionCommand {
    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "mapPartNumbers" );

        HashMap errors = new HashMap ();
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );

        MapPartNumbersForm mapPartNumbersForm = ( MapPartNumbersForm )form;
        MapPartNumbers mapPartNumbers = mapPartNumbersForm.getMapPartNumbers();
        ArrayList chipsList = mapPartNumbers.getChipsList();
        ArrayList boardsList = mapPartNumbers.getBoardsList();
        ArrayList kitsList = mapPartNumbers.getKitsList();
        try
        {
            request.setAttribute( "availableChipsList" , ProjectSetupDAO.loadAllProducts( null, "CHIP") );
            request.setAttribute( "availableBoardsList" , ProjectSetupDAO.loadAllProducts( null, "BOARD") );
            request.setAttribute( "availableKitsList" , ProjectSetupDAO.loadAllProducts( null, "KIT") );

        } catch( Exception e )
        {
            e.printStackTrace();
        }
        if ( chipsList.size() > 0 )
        {
            request.setAttribute("showChips", "TRUE");
        }
        if ( boardsList.size() > 0 )
        {
            request.setAttribute("showBoards", "TRUE");
        }
        if ( kitsList.size() > 0 )
        {
            request.setAttribute("showKits", "TRUE");
        }
        return actionForward;
    }

}
